package com.sdcqjy.property.widget;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.sdcqjy.property.activity.WebViewActivity;
import com.sdcqjy.property.base.BannerMode;
import com.sdcqjy.property.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：
 * Created by rookieCi on 2017/9/20.
 */
public class BannerView {
    private int time = 5000;
    private Context context;
    private List<BannerMode> imageViews;
    private MyAdapter myAdapter;
    private ViewPager viewPager;
    private OnClickImageListener onClickImageListener;
    private boolean isViewPagerOnTouch;
    private boolean isRunning = true;
    private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                case MotionEvent.ACTION_DOWN:
                    isViewPagerOnTouch = true;
                    break;
                case MotionEvent.ACTION_UP:
                    isViewPagerOnTouch = false;
                    break;
            }
            return false;
        }
    };
    private Handler imgHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (isRunning) {
                imgHandler.sendEmptyMessageDelayed(0, time);
                if (!isViewPagerOnTouch)
                    if (imageViews.size() > 0)
                        viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
            }
        }
    };

    public BannerView(Context context, ViewPager viewPager) {
        this.context = context;
        this.viewPager = viewPager;
        imageViews = new ArrayList<>();
        myAdapter = new MyAdapter();
        viewPager.setAdapter(myAdapter);
        viewPager.setOnTouchListener(onTouchListener);
        imgHandler.sendEmptyMessageDelayed(0, time);
    }

    public void addImageViews(List<BannerMode> images) {
        imageViews.clear();
        imageViews.addAll(images);
        myAdapter.notifyDataSetChanged();
    }

    public BannerView setOnClickImageListener(OnClickImageListener onClickImageListener) {
        this.onClickImageListener = onClickImageListener;
        return this;
    }

    public BannerView setTime(int time) {
        this.time = time;
        return this;
    }

    public void onResume() {
        isViewPagerOnTouch = true;
    }

    public void onPause() {
        isViewPagerOnTouch = false;
    }

    public void onDestroy() {
        isRunning = false;
        imgHandler.removeCallbacksAndMessages(null);
    }

    public class MyAdapter extends PagerAdapter {
        public int getCount() {
            return imageViews.size() == 0 ? 0 : Integer.MAX_VALUE;
        }

        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            int i = position % imageViews.size();
            imageView.setId(i);
            GlideUtils.displayOfUrl(context, imageView, imageViews.get(i).image);
            GlideUtils.removeView(imageView);
            container.addView(imageView);
            imageView.setOnClickListener(v -> {
                int id = v.getId();
                BannerMode bannerMode = imageViews.get(id);
                if (onClickImageListener != null) {
                    onClickImageListener.onClickImage(bannerMode);
                } else if (!TextUtils.isEmpty(bannerMode.slide_url))
                    WebViewActivity.open(context, bannerMode.title, bannerMode.slide_url);
            });
            return imageView;
        }

        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }
    }

    public interface OnClickImageListener {
        public void onClickImage(BannerMode mode);
    }
}